package greedy;

/**
 * Created with IntelliJ IDEA.
 * Description: 135. 分发糖果
 * User: Li_yizYa
 * Date: 2025/6/26
 * Time: 16:25
 */
public class Solution9 {
    public int candy(int[] ratings) {
        if (ratings == null || ratings.length == 0) {
            return -1;
        }
        int[] arr = new int[ratings.length];
        arr[0] = 1;
        for (int i = 1; i < ratings.length; i++) {
            arr[i] = ratings[i] > ratings[i - 1] ? arr[i - 1] + 1 : 1;
        }
        int ret = 0;
        for (int i = arr.length - 1; i >= 1; i--) {
            if (ratings[i - 1] > ratings[i] && arr[i - 1] <= arr[i]) {
                arr[i - 1] = arr[i] + 1;
            }
            ret += arr[i];
        }
        return ret + arr[0];
    }
}
